let min_H = $(window).height() - $('.navbar').height() - $('body').children('.container').children('.page-header').height();
$('body').children('.container').children('.main-pd-10').css('min-height',min_H);
/***************************************************/
// 页面初加载时，获取用户信息
let userInfo = getCookie();
console.log(userInfo);
Boolean( userInfo ) ? navBarInfo(userInfo) : console.log('未登录');
/***************************************************/
let OFF_getAD = false;
let searchInfo = '';// 搜索信息
let AD_sort = 0;// 公告排序
let dataOBJ = {
    pageNum : 1,
    pageSize : 9,
    startTime : '',
    stopTime : '',
    name : '',
    time_sort : AD_sort,
    pages : 0 ,
};

// 退出登录
$('#logon').on('click',function (){
    let OFF = $(this).attr('data-id');
    if( Boolean( Number(OFF) ) ){
        if( confirm('确认登录退出？') ){
            outLoing();
        }
    }
});

//密码按钮开关
$('.look-off').on('click', function(){
    if( $(this).hasClass('glyphicon-eye-close') ){
        $(this).removeClass('glyphicon-eye-close');
        $(this).addClass('glyphicon-eye-open Text-red');
        $(this).prev().attr('type','text');
    }else{
        $(this).removeClass('glyphicon-eye-open Text-red');
        $(this).addClass('glyphicon-eye-close');
        $(this).prev().attr('type','password');
    }
});

//性别按钮CSS变换
$('.radio-change-sex').on('click',function(){
    console.log($(this).find('input').val());
    $(this).siblings().removeClass('btn-primary').addClass('btn-default');
    $(this).addClass('btn-primary');
});

// 搜索框
$('#inputSuccess2').keydown(function (ev) {
    switch (ev.keyCode){
        case 13:
            // 搜索页面跳转
            searchTargetPage();
    }
});

// 搜索页面跳转
function searchTargetPage() {
    searchInfo = $('#inputSuccess2').val();
    if( searchInfo != '' ){
        if( window.location.pathname != '/search-index.html' ){
            window.location.href = '/search-index.html?name='+searchInfo
        }else{
            thisSearchPage( searchInfo );
        }
    }else{
        spop_waring( '输入不能为空',function () { },function () { } );
    }
}
function initSearchPage() {
    let DATA = getSearchInfo();
    $.ajax({
        url : '/announcemen/search_index.do',
        data : DATA,
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        success : function (data) {
            if( data.status == 0 ){
                To_SearchList( data.data, DATA.name );
            }else {
                spop_waring( data.msg);
            }
        }
    });
}
function thisSearchPage( searchInfo ) {
    $('#searchList').html('');
    $.ajax({
        url : '/announcemen/search_index.do',
        data : {
            name : searchInfo
        },
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        success : function (data) {
            console.log(data);
            if( data.status == 0 ){
                To_SearchList( data.data, searchInfo );
            }else if(data.status == 10 ){
                spop_waring( data.msg, function(){}, outLoing );
            }
        }
    })
}
function To_SearchList( aData, searchTxt ) {
    if( aData.length ){
        for(let i=0; i<aData.length; i++){
            let title = aData[i].name.replace(eval("/"+ searchTxt+"/gi"), '<mark>'+ searchTxt +'</mark>');
            let str = aData[i].xiangqin.replace( eval("/"+ searchTxt+"/gi"), '<mark>'+ searchTxt +'</mark>');
            let DOM = $('<a href="'+aData[i].url +'" class="list-group-item ">' +
                '<h4 class="list-group-item-heading">'+ title +'</h4>' +
                '<p class="list-group-item-text">'+ str +'</p>' +
                '</a>');
            DOM.appendTo( $('#searchList') );
        }
    }else{
        let DOM = $('<a href="javascript:;" class="list-group-item list-group-item-danger ">' +
            '<h4 class="list-group-item-heading ">未搜索到任何数据</h4>' +
            '</a>');
        DOM.appendTo( $('#searchList') );
    }

    $('#inputSuccess2').val('').attr('placeholder',searchTxt);
}
// navBar 渲染用户信息
function navBarInfo(data){
    $('#zhuCe').html($('<i class="glyphicon glyphicon-user "></i><span class="mar-5">'+ data.stuname +'</span>'))
        .attr('href','./updateInfo.html');

    $('#logon').html($('<i class="glyphicon glyphicon-off "></i><span class="mar-5">退出</span>'))
        .attr({
            'href' : 'javascript:;',
            'data-ID' : '1'
        });
}
// 退出登录
function outLoing() {
    // removeCookie();
    // window.location.replace('/logon.html');

    $.ajax({
        url : '/user/logout.do',
        type:'get',
        data : {
        },
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        async : false,// 同步
        success: function (data) {
            console.log(data);
            if( data.status == 0){
                spop_ok( data.msg, removeCookie, function () {
                    window.location.replace('/logon.html');
                } );
            }else{
                spop_ok('退出成功', removeCookie, function () {
                    window.location.replace('/logon.html');
                });
            }
        },
        error : function (data) {
            spop_ok('退出成功', removeCookie, function () {
                window.location.replace('/logon.html');
            });
        }
    });
}
// 再次获取用户信息
function getUserInfo( callBack ){
    $.ajax({
        url : '/user/get_information.do',
        type : 'post',
        data : {},
        dataType : 'json',
        contentType : 'application/x-www-form-urlencoded',
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        success : function (data) {
            if (data.status == 0) {
                window.Cookie.clear();
                window.Cookie.setItem('jdzUserInfo', JSON.stringify(data.data));
                if( typeof callBack == 'function'){
                    callBack(data.data);
                }
            }else if( data.status == 10 ){
                spop_waring( data.msg, function () {}, outLoing );
            }
        }
    });
}
// 蒙版-全屏
function mask_open(){
    $.mask_fullscreen();
}
// 关闭 蒙版
function mask_close(){
    $.mask_close_all();
}
// 获取input[file]内文件 的路径
function getObjectURL(file) {
    let url = null;
    if (window.createObjcectURL != undefined) {
        url = window.createOjcectURL(file);
    } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

/***********************************************/
/***********************************************/
/***********************************************/

// 公告排序 --ok
$('#AD_sort').on('click',function () {
    dataOBJ.time_sort = dataOBJ.time_sort == 0 ? 1 : 0;
    getAdList();
});
function getAdList() {
    let str = dataOBJ.name ;
    // 搜索模式
    if( str ){
        if(!OFF_getAD){ // !== true
            OFF_getAD = true;
            dataOBJ.pageNum = 1;
        }
    }
    // 非搜索模式
    else{
        if(OFF_getAD){ // !== true
            OFF_getAD = false;
            dataOBJ.pageNum = 1;
        }
    }

    $.ajax({
        type: 'post',
        url : '/announcemen/list_announcemen.do',
        data : {
            pageNum : dataOBJ.pageNum,
            pageSize : dataOBJ.pageSize,
            startTime : dataOBJ.startTime,
            stopTime : dataOBJ.stopTime,
            name : dataOBJ.name,
            time_sort : dataOBJ.time_sort
        },
        dataType : 'json',
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        success: function (data) {
            console.log(data);
            if(data.status == 0){
                // 渲染公告列表 ==> 页码
                To_AdList( data.data.list );
                To_pagingInfo( data.data );
            }else {
                spop_waring( data.msg )
            }
        }
    });
}
// 渲染公告列表 --ok
function To_AdList(data){
    $('#adList').html('');
    if( data.length > 0 ){
        for(let i=0; i<data.length; i++ ){
            let DOM = $('<tr>' +
                '<td class="Text-tac" >' +
                    '<i class="glyphicon glyphicon-tags Text-red"></i>' +
                '</td>' +
                '<td>'+ data[i].creattime +'</td>' +
                '<td>' +
                    '<a href="/noticeBoard.html?annno='+ data[i].annno +'" class="">'+ data[i].annid +'</a>' +
                '</td>' +
                '</tr>');

            DOM.appendTo( $('#adList') );
        }
    }else{
        $('<tr><td></td><td></td><td>暂无信息</td></tr>').appendTo( $('#adList') );
    }
}
// 公告详情
$('#adList').on('click','a',function(){
    let itemId = $(this).attr('data-id');
    $.ajax({
        type: 'post',
        url : '/announcemen/details_announcemenn.do',
        data : {
            annno : itemId
        },
        dataType : 'json',
        xhrFields : {
            withCredentials: true
        },
        crossDomain: true,
        success: function (data) {
            if(data.status == 0){
                window.Cookie.setItem('web-AD-info',JSON.stringify(data.data));
                window.location.href = './noticeBoard.html';
            }else if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing );
            }
        },
        error : function (){
            spop_er('公告详情请求出错');
        }
    });
});
/***********************************************/
/***********************************************/
/***********************************************/

// 页码获取 --ok
function To_pagingInfo(data){
    dataOBJ.pages = data.pages;// 总共页数
    dataOBJ.pageNum = data.pageNum;// 当前页数
    $('.panel-footer').find('.paging-r').children('span').eq(0).text(dataOBJ.pageNum);
    $('.panel-footer').find('.paging-r').children('span').eq(1).text(dataOBJ.pages);
    $('.panel-footer').find('.paging-r').children('span').eq(2).text(data.total);
    $('#pageNumber-page').attr({
        placeholder : dataOBJ.pageNum,
        max : dataOBJ.pages
    });
}
// 页码切换 --ok
function pagingChange() {
    $('#first-page').on('click',function () {
        // 判断是否为第一页
        if( dataOBJ.pageNum == 1 ){
            spop_waring('已经是第一页了');
            return false;
        }else{
            dataOBJ.pageNum = 1;
            getAdList();
        }
    });
    $('#prev-page').on('click',function () {
        // 判断是否为第一页
        if( dataOBJ.pageNum == 1 ){
            spop_waring('已经是第一页了');
            return false;
        }else{
            dataOBJ.pageNum--;
            getAdList();
        }
    });
    $('#next-page').on('click',function () {
        // 判断是否为最后一页
        if( dataOBJ.pageNum == dataOBJ.pages){
            spop_waring('已经是最后一页了');
            return false;
        }else{
            dataOBJ.pageNum++;
            getAdList();
        }
    });
    $('#last-page').on('click',function () {
        // 判断是否为最后一页
        if( dataOBJ.pageNum == dataOBJ.pages){
            spop_waring('已经是最后一页了');
            return false;
        }else{
            dataOBJ.pageNum = dataOBJ.pages;
            getAdList();
        }
    });
}
// 页脚输入框事件绑定 --ok
$('#pageNumber-page').keyup(function(ev){
    if( $(this).val() > dataOBJ.pages ){
        $(this).val(dataOBJ.pages);
    }
    switch (ev.keyCode){
        case 13 :
            dataOBJ.pageNum = $(this).val();
            getAdList();
            break;
        default :
            break;
    }
}).blur(function (ev) {
    $(this).attr({
        placeholder: dataOBJ.pageNum
    }).val('');
});
// 储存用户信息 -- ok
function setCookie(data) {
    removeCookie();
    $.cookie( 'jdzUserInfo_cookie', escape(JSON.stringify(data)), {
        path:'/'
    } );
}
function getCookie() {
    // alert('设置');
    if( $.cookie('jdzUserInfo_cookie') ){
        return  JSON.parse(unescape($.cookie('jdzUserInfo_cookie')));
    }else{
        return false;
    }
}
function removeCookie() {
    $.cookie( 'jdzUserInfo_cookie', null , {
        expires : -1,
        path    : '/'
    } );
}
function getSearchInfo() {
    let url = location.search; //获取url中"?"符后的字串
    let obj = new Object();
    if(url.indexOf("?") != -1) {//判断？后面是否有字符
        let str = url.substr(1);//从第一个字符开始截取
        str = str.split("&");//以&符号分离
        for(let i = 0; i < str.length; i++) {
            //decodeURI：将字符解码
            //decodeURIComponent：加密
            obj[str[i].split("=")[0]] = decodeURI(str[i].split("=")[1]);
        }
    }
    return obj;
}
// 消息框
function spop_ok( status, openFn, colseFn ) {
    spop({
        template: status,
        position  : 'top-center',
        style:'success',
        autoclose : 1500,
        group: 'spop-Group',
        onOpen : function () {
            if( typeof openFn == 'function'){
                openFn();
            }
        },
        onClose: function() {
            if( typeof colseFn == 'function'){
                colseFn();
            }
        }
    });
}
function spop_er( status, openFn, colseFn ) {
    spop({
        template: status,
        position  : 'top-center',
        style:'error',
        autoclose : 1500,
        group: 'spop-Group',
        onOpen : function () {
            if( typeof openFn == 'function'){
                openFn();
            }
        },
        onClose: function() {
            if( typeof colseFn == 'function'){
                colseFn();
            }
        }
    });
}
function spop_waring( status, openFn, colseFn ) {
    spop({
        template: status,
        position  : 'top-center',
        style:'warning',
        autoclose : 1500,
        group: 'spop-Group',
        onOpen : function () {
            if( typeof openFn == 'function'){
                openFn();
            }
        },
        onClose: function() {
            if( typeof colseFn == 'function'){
                colseFn();
            }
        }
    });
}